﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InfoPathTranslate
{
    public enum InfoPathFieldType
    {
        String,
        XHTML,
        Integer,
        Double,
        Boolean,
        AnyURI,
        Date,
        Time,
        DateTime,
        Base64,
        ComplexType
    }

    public abstract class InfoPathField
    {
        abstract public InfoPathFieldType GetFieldType();

        abstract public String GetStringValue();

        abstract public void SetValue(String v);

        virtual public Boolean Equels(InfoPathField f)
        {
            return false;
        }
        virtual public Boolean NotEquels(InfoPathField f)
        {
            return false;
        }
        virtual public Boolean GreaterThan(InfoPathField f)
        {
            return false;
        }
        virtual public Boolean LessThan(InfoPathField f)
        {
            return false;
        }

    }

    public class InfoPathBoolean : InfoPathField
    {
        private bool value;

        public InfoPathBoolean(bool v)
        {
            this.value = v;
        }
        override public String GetStringValue()
        {
            if (value)
            {
                return "True";
            }
            else
            {
                return "False";
            }
        }

        override public void SetValue(String v)
        {
            if (v.Equals("1") || v.ToLower().Equals("true"))
            {
                this.value = true;
            }
            else
            {
                this.value = false;
            }
        }

        override public InfoPathFieldType GetFieldType()
        {
            return InfoPathFieldType.Boolean;
        }

        override public Boolean Equels(InfoPathField f)
        {
            if (f.GetFieldType() == InfoPathFieldType.String)
            {
                bool b = f.GetStringValue().ToLower().Equals("true");
                return this.value == b;
            }
            if (f.GetFieldType() != InfoPathFieldType.Boolean)
            {
                return false;
            }
            return this.value == ((InfoPathBoolean)f).value;
        }

        override public Boolean NotEquels(InfoPathField f)
        {
            return !this.Equals(f);
        }

        public bool GetBooleanValue()
        {
            return this.value;
        }

        public void SetBooleanValue(bool v)
        {
            this.value = v;
        }
    }

    public class InfoPathString : InfoPathField
    {
        private String value;

        public InfoPathString(String v)
        {
            this.value = v;
        }
        override public String GetStringValue()
        {
            return value;
        }

        override public void SetValue(String v)
        {
            this.value = v;
        }

        override public InfoPathFieldType GetFieldType()
        {
            return InfoPathFieldType.String;
        }

        override public Boolean Equels(InfoPathField f)
        {
            if (f.GetFieldType() == InfoPathFieldType.Boolean)
            {
                bool b = this.value.ToLower().Equals("true");
                return ((InfoPathBoolean)f).GetBooleanValue() == b;
            }
            else if (f.GetFieldType() == InfoPathFieldType.String)
            {
                return this.value.Equals(f.GetStringValue());
            }
            return false;
        }

        override public Boolean NotEquels(InfoPathField f)
        {
            return !this.Equals(f);
        }
    }
}
